import { removeRule, addRule, updateRule, handleAccount } from '@/services/api';
import { queryWxUsersList, } from '@/services/user';
export default {
  namespace: 'users',

  state: {
    wxUserMap: {
      list: [],
      pagination: {},
    },
    userInfo: {},
  },
  effects: {
    *fetchWxUser({ payload }, { call, put }) {
      const response = yield call(queryWxUsersList, payload);
      yield put({
        type: 'save',
        payload: {
          list: response.data.data,
          pagination: {
            total: response.data.total,
            pageSize: 10,
            current: parseInt(response.data.currentPage, 10) || 1,
          },
        },
      });
    },
 
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addRule, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeRule, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateRule, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    *handle({ payload, callback }, { call, put }) {
      const response = yield call(handleAccount, payload);
      if (response.code === 200) {
        let response = yield call(queryUsersList, payload);

        yield put({
          type: 'save',
          payload: {
            list: response.data.data,
            pagination: {
              total: response.data.total,
              pageSize: 10,
              current: parseInt(response.data.currentPage, 10) || 1,
            },
          },
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        wxUserMap: action.payload,
      };
    },
  
  },
};
